import { userLogin } from './../utils/userApi';
/**
 * 跳转到页面
 * @param {*} param0 Event currenttarget的datset上需要有url
 * @param {*} query url后跟的参数对象
 * @param {*} type 导航类型
 */
export default function gotoPage({ currentTarget }, query, type = 'navigateTo') {
    let url = currentTarget && currentTarget.dataset && currentTarget.dataset.url;
    if (verifyLoginStatus(url)) {
        return false
    } else {
        wx[type]({
            url: url 
        })
    }

}

const NEED_LOGIN_PAGE = [
    '/mineSubPackage/pages/viewingRecord/viewingRecord',
    '/mineSubPackage/pages/followRecord/followRecord'
];

/**
 * 验证跳转页面是否需要登录
 * @param {*} url 要跳转的页面 绝对路径
 * @param {*} type 1 若需登录只需要弹出提示框
 */
function verifyLoginStatus(url, type = 1) {
    if(url.indexOf('?') != -1){
        url = url.substring(0,url.indexOf('?'))
    }
    if (NEED_LOGIN_PAGE.includes(url)) {
        if (type === 1 && !getLoginStatus()) {
            wx.showModal({
                title: '提示',
                content: '检测到您还未登录，请先登录！',
                confirmText: '确定',
                cancelText: '取消',
                success(res) {
                    if (res.confirm) {
                        wx.switchTab({
                          url: '/pages/mine/mine',
                        })
                        // userLogin();
                    } else if (res.cancel) {
                        wx.showToast({
                            title: '取消登录，部分功能将无法使用！',
                            icon: "none"
                        })
                    }
                }
            })
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

//获取当前是否登录
function getLoginStatus() {
    let token = wx.getStorageSync('token');
    // console.log(token);
    if (token) return true;
    return false;
}